package com.jingyanzi.privilege.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.jingyanzi.privilege.dao.EmployeeSalesGradeMapper;
import com.jingyanzi.privilege.domain.SalesGradeDO;
import com.jingyanzi.privilege.domain.result.ResultDO;
import com.jingyanzi.privilege.domain.result.ResultSupport;
import com.jingyanzi.privilege.entity.EmployeeSalesGrade;
import com.jingyanzi.privilege.entity.EmployeeSalesGradeExample;
import com.jingyanzi.privilege.entity.EmployeeSalesGradeExample.Criteria;
import com.jingyanzi.privilege.query.SalesGradeQueryDO;
import com.jingyanzi.privilege.service.ISalesGradeService;
import com.jingyanzi.privilege.util.BeanUtilsExtends;

public class SalesGradeServiceImpl extends BaseServiceImpl implements ISalesGradeService {
	
	@Autowired
	private EmployeeSalesGradeMapper employeeSalesGradeMapper;


	@Override
	public ResultDO add(SalesGradeDO domain) {
		
		EmployeeSalesGrade entity = new EmployeeSalesGrade();
		
		ResultSupport result = BeanUtilsExtends.copy(entity, domain);
		int r = -1;
		entity.setCreatetime(Calendar.getInstance().getTime());
		entity.setModifitime(entity.getCreatetime());
		entity.setIsDelete(1);

		try {
			r = employeeSalesGradeMapper.insertSelective(entity);
		} catch (Exception e) {
			e.printStackTrace();
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("添加销售级别失败");
			result.setSuccess(false);
			return result;
		}
		if (r < 0) {
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("添加销售级别失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, entity.getId());

		return result;
	}

	@Override
	public ResultDO modifi(SalesGradeDO domain) {
		
		EmployeeSalesGrade entity = new EmployeeSalesGrade();
		
		ResultSupport result = BeanUtilsExtends.copy(entity, domain);
		int r = -1;
		
		entity.setModifitime(entity.getCreatetime());
		
		try {
			r = employeeSalesGradeMapper.updateByPrimaryKeySelective(entity);
		} catch (Exception e) {
			e.printStackTrace();
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("修改销售级别失败");
			result.setSuccess(false);
			return result;
		}
		if (r < 0) {
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("修改销售级别失败");
			result.setSuccess(false);
			return result;
		}

		return result;
	}

	@Override
	public ResultDO get(Integer id) {
		
		EmployeeSalesGrade entity = null;
		
		ResultSupport result = new ResultSupport();
	
		try {
			entity = employeeSalesGradeMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("获取销售级别失败");
			result.setSuccess(false);
			return result;
		}
		if (entity != null) {
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("获取销售级别失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, getSalesGradeDO(entity));

		return result;
	}

	@Override
	public ResultDO remove(Integer id) {
		
		ResultSupport result = new ResultSupport();
		
		EmployeeSalesGrade entity = new EmployeeSalesGrade();
		
		int r = -1;
		
		entity.setId(id);
		entity.setIsDelete(-1);
		try {
			r = employeeSalesGradeMapper.updateByPrimaryKeySelective(entity);
		} catch (Exception e) {
			e.printStackTrace();
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("删除销售级别失败");
			result.setSuccess(false);
			return result;
		}
		if (r < 0) {
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("删除销售级别失败");
			result.setSuccess(false);
			return result;
		}

		return result;
	}

	@Override
	public ResultDO query(SalesGradeQueryDO query) {
		
		ResultSupport result = new ResultSupport();

		List<EmployeeSalesGrade> list = null;

		EmployeeSalesGradeExample example = new EmployeeSalesGradeExample();
		
		Criteria createCriteria = example.createCriteria();
		

		createCriteria.andIsDeleteEqualTo(1);

		List<SalesGradeDO> doList = null;

		if (query.isPage()) {
			example.setLimitByPage(query.getStart() + "," + query.getPageRows());
			long count = employeeSalesGradeMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}
		
		try {
			list = employeeSalesGradeMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("获取组织机构信息失败");
			result.setSuccess(false);
			return result;
		}

		doList = getSalesGradeDOList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, doList);

		return result;
	}

	private SalesGradeDO getSalesGradeDO(EmployeeSalesGrade obj) {
		if (obj == null) return null;
		SalesGradeDO dst = new SalesGradeDO();
        return BeanUtilsExtends.copyProperties(dst, obj) ? dst : null;
	}
	
	
	private List<SalesGradeDO> getSalesGradeDOList(List<EmployeeSalesGrade> list) {
		List<SalesGradeDO> result = new ArrayList<SalesGradeDO>();
		if (list != null && !list.isEmpty()) {
			for (EmployeeSalesGrade org : list) {
				SalesGradeDO org1 = getSalesGradeDO(org);
				if (org1 != null) {
					result.add(org1);
				}
			}
		}
		return result;
	}
}
